//
//  AppDelegate.swift
//  XGamesLive
//
//  Created by ios on 2024/5/13.
//

import UIKit
import IQKeyboardManagerSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var allowRotation: Bool = false
    var scapeType: UIInterfaceOrientationMask = .portrait

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        UIApplication.shared.applicationIconBadgeNumber = 0
        if #available(iOS 15.0, *) {
            UITableView.appearance().sectionHeaderTopPadding = 0
        }
        XGL_NetworkStatusManager.shared.start()
        XGL_PurchaseManager.share.xgl_transactions()
        XGLTabbarPlusButton.register()
        
        PanTimer.share.creatNoticeTimer()

        IQKeyboardManager.shared.enable = true

        MainViewController.selectedIndex = 0
        self.window?.rootViewController = MainViewController
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.portraitScreenNotice), name: DeviceDirectionPortraitScreenNotice, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.landscapeScreenNotice), name: DeviceDirectionLandscapeScreenNotice, object: nil)
        return true
    }


    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if self.allowRotation {
            return .all
        }
        return self.scapeType
    }
}

extension AppDelegate{
    @objc private func portraitScreenNotice(){
        self.scapeType = .portrait
        let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
        UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
        
        let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.portrait.rawValue)
        UIDevice.current.setValue(orientationTarget, forKey: "orientation")
    }
    @objc private func landscapeScreenNotice(){
        self.scapeType = .landscapeRight
        let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
        UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
        
        let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.landscapeRight.rawValue)
        UIDevice.current.setValue(orientationTarget, forKey: "orientation")
    }
}
